package com.itheima.until;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUntil {
        private JDBCUntil(){}

        private static String className;
        private static String url;
        private static String passwrod;
        private static String username;

        static{
            try {
                InputStream is = JDBCUntil.class.getClassLoader().getResourceAsStream("config.properties");
                Properties properties = new Properties();
                properties.load(is);

                className = properties.getProperty("ClassName");
                url = properties.getProperty("url");
                username = properties.getProperty("username");
                passwrod = properties.getProperty("passwrod");

                Class.forName(className);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        public static Connection getConnection() throws SQLException {
            return  DriverManager.getConnection(url,username,passwrod);
        }
        public static  void close(Connection conn , Statement stat , ResultSet rs){
            if(conn!= null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat!= null){
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!= null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    public static  void close(Connection conn , Statement stat ){
        if(conn!= null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stat!= null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
